package com.dnake.receiver;

import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;

import com.dnake.WeKerApplication;
import com.dnake.entity.EventBusGsmBean;

import org.greenrobot.eventbus.EventBus;

import static com.dnake.utils.NetUtil.getAPNType;


/**
 * @author haoyuliu
 * @since 2017/7/26
 */

public class PhoneStatListener extends PhoneStateListener {
    //获取信号强度
    public static final int SIGNAL_STRENGTH_NONE_OR_UNKNOWN = 0;
    /** @hide */
    public static final int SIGNAL_STRENGTH_POOR = 1;
    /** @hide */
    public static final int SIGNAL_STRENGTH_MODERATE = 2;
    /** @hide */
    public static final int SIGNAL_STRENGTH_GOOD = 3;
    /** @hide */
    public static final int SIGNAL_STRENGTH_GREAT = 4;
    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        //获取0-4的5种信号级别，越大信号越好,但是api23开始才能用
         int level = 0;
        int asu = signalStrength.getGsmSignalStrength();
        //获取网络类型
        int netWorkType = getAPNType(WeKerApplication.getContext());
        if (asu <= 2 || asu == 99) level = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
        else if (asu >= 12) level = SIGNAL_STRENGTH_GREAT;
        else if (asu >= 8)  level = SIGNAL_STRENGTH_GOOD;
        else if (asu >= 5)  level = SIGNAL_STRENGTH_MODERATE;
        else level = SIGNAL_STRENGTH_POOR;
        EventBus.getDefault().postSticky(new EventBusGsmBean(level,netWorkType));
    }
}
